<?php
App::uses('AppController', 'Controller');
  
/**
 * Consignments Controller
 *
 * @property Consignment $Consignment
 */
class ConsignmentsController extends AppController {



/**
 * index method
 *
 * @return void
 */
	public function index() {
		
		$this->Consignment->recursive = 0;
		$this->set('consignments', $this->paginate("Consignment"));
		
	//	$this->set('ConsignmentItem', $this->Image->find('all'));
		
	}
	
	public function indexdata() {	

        $this->autoRender = false;  
		$output = $this->Consignment->GetData();	
		echo json_encode($output);
		
	}

	

/**
 * view method
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		if (!$this->Consignment->exists($id)) {
			throw new NotFoundException(__('Invalid consignment'));
		}
				
		
		$options = array('conditions' => array('Consignment.' . $this->Consignment->primaryKey => $id));
		$this->set('consignment', $this->Consignment->find('first', $options));

	
		$ConsignmentItem = $this->loadModel('ConsignmentItem');
		
		$options = array (
					"conditions" => array (
							"ConsignmentItem.consignment_id" => $id,
					) 
			);
		$consignmentItem = $this->ConsignmentItem->find ( "all",$options );		
		$this->set('consignmentItem', $consignmentItem);
		
		
		
	}


	public function viewitem() {
	
		$ConsignmentItem = $this->loadModel('ConsignmentItem');
		
		$id = $this->request->params['pass'][0];
		if ($this->request->is ( "ajax" )) {
			$this->layout = null;
		}
		$this->set ( "id", $id );
		
		$consignmentItem = array ();
		$itemlist = array ();
		$status=false;

		$options = array (
					"conditions" => array (
							"ConsignmentItem.consignment_id" => $id
					) 
			);
			
		$consignmentItem = $this->ConsignmentItem->find ( "all",$options );
				
		foreach ( $consignmentItem as $v ) {$itemlist[]=$v["Item"];	}	
		
		$items = "";
		foreach ( $itemlist as $item ) {		
			$items .= "<a href='items/view/".$item['id']."'>".$item['name']."</a>,";
		}
		
		echo $items = rtrim($items,",");
		
		die ();
		
		
			}
	
	
	
/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Consignment->create();
			
			$this->Consignment->set ( $this->data );
			$this->Consignment->ConsignmentItem->set ( $this->data );
						
			$this->data = Set::filter ( $this->data );
			$result = $this->Consignment->saveAll ( $this->request->data );
			
			$this->Consignment->saveField('date', date('Y-m-d H:i:s')); 
	
			
			if ($result) {			
			$this->Session->setFlash ( __ ( 'Added successful' ) );	

                $this->redirect ( array (
                    'action' => 'index'
                ) );	
			} else {
				$this->Session->setFlash(__('The consignment could not be saved. Please, try again.'));
			}
		}
		
		$items = $this->Consignment->Item->find('list');
        $brands = $this->Consignment->Item->Brand->find('list');
        $colors = $this->Consignment->Item->Color->find('list');
        $conditions = $this->Consignment->Item->Condition->find('list');
        $consigners = $this->Consignment->Consigner->find('list');
        $sizes = $this->Consignment->Item->Size->find('list');
		
		$Item = $this->loadModel('Item');
		$itemStatuses = $this ->Item->ItemStatus-> find('list', array('order' => 'ItemStatus.name asc'));
        $categories = $this->Consignment->Item->Category->find('list');
        /*$itemStatuses = $this ->consignement->Item->Itemstatus('list')*/
        $this->set(compact('items', 'consigners', 'brands', 'colors', 'conditions', 'sizes','categories','itemStatuses'));

	}
	
	public function itemdropdown()
	{
		header ( "Pragma: no-cache" );
		header ( "Cache-Control: no-store, no-cache, max-age=0, must-revalidate" );
		header ( 'Content-Type: text/x-json' );
		header ( 'Content-type: application/json' );
		$itemlist = array ();
		$status=false;

		$item_all = $this->Consignment->Item->find('all');
				
		if (! empty ( $item_all ))
		{
			foreach ( $item_all as $v ) {$itemlist[] =$v["Item"];}						
		}
		
		$data = array (
			"itemlist" => $itemlist
		); 
		
		echo json_encode ( $data );
		die ();
		
		$items = $this->Consignment->Item->find('list');
        $this->set(compact('items'));
	}


/**
 * edit metho   d
 *
 * @throws NotFoundException
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		if (!$this->Consignment->exists($id)) {
			throw new NotFoundException(__('Invalid consignment'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Consignment->save($this->request->data)) {
												
			$ConsignmentItem = $this->loadModel('ConsignmentItem');	
			$this->ConsignmentItem->deleteAll(array('ConsignmentItem.consignment_id' => $id), false);
			
			$this->Consignment->ConsignmentItem->set ( $this->data );
						
			$this->data = Set::filter ( $this->data );
			$result = $this->Consignment->saveAll ( $this->request->data );
						
			$this->Session->setFlash(__('The consignment has been saved'));
			$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The consignment could not be saved. Please, try again.'));
			}
		} else {
		
			$options = array('conditions' => array('Consignment.' . $this->Consignment->primaryKey => $id));
			$this->request->data = $this->Consignment->find('first', $options);
			
			$ConsignmentItem = $this->loadModel('ConsignmentItem');
			
			$options = array (
				"conditions" => array (
					"ConsignmentItem.consignment_id" => $id,
				) 
			);
			
			$consignmentItem = $this->ConsignmentItem->find ( "all",$options );		
			$this->set('item', $consignmentItem);
			
		}
		
		$items = $this->Consignment->Item->find('list');
        $brands = $this->Consignment->Item->Brand->find('list');
        $colors = $this->Consignment->Item->Color->find('list');
        $conditions = $this->Consignment->Item->Condition->find('list');
        $consigners = $this->Consignment->Consigner->find('list');
        $sizes = $this->Consignment->Item->Size->find('list');
		$Item = $this->loadModel('Item');
		$itemStatuses = $this ->Item->ItemStatus-> find('list', array('order' => 'ItemStatus.name asc'));
        $categories = $this->Consignment->Item->Category->find('list');
        /*$itemStatuses = $this ->consignement->Item->Itemstatus('list')*/
        $this->set(compact('items', 'consigners', 'brands', 'colors', 'conditions', 'sizes','categories','itemStatuses'));
	}

/**
 * delete method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
	
		$this->Consignment->id = $id;
		if (!$this->Consignment->exists()) {
			throw new NotFoundException(__('Invalid consignment'));
		}
		$this->request->onlyAllow('post', 'delete');
		if ($this->Consignment->delete()) {
			$this->Session->setFlash(__('Consignment deleted'));
			
		$ConsignmentItem = $this->loadModel('ConsignmentItem');	
		$this->ConsignmentItem->deleteAll(array('ConsignmentItem.consignment_id' => $id), false);
		
		
			$this->redirect(array('action' => 'index'));
		}
		
	

		$this->Session->setFlash(__('Consignment was not deleted'));
		$this->redirect(array('action' => 'index'));
	}
/**
 * Receipt method
 */
	public function receipt($consignment_id)
	{
		/*$item=$this->Consignment->find('first',array('conditions'=>array('Consignment.id'=>$consignment_id)));
		$this->set('item',$item);*/
		
		$options = array('conditions' => array('Consignment.' . $this->Consignment->primaryKey => $consignment_id));
		$this->set('consignment', $this->Consignment->find('first', $options));

		$ConsignmentItem = $this->loadModel('ConsignmentItem');
		
		$options = array (
					"conditions" => array (
							"ConsignmentItem.consignment_id" => $consignment_id,
					) 
			);
		$consignmentItem = $this->ConsignmentItem->find ( "all",$options );		
		$this->set('item', $consignmentItem);
		
		
//		echo "<pre>"; print_r($consignmentItem);exit;
		
	
	}
	
	/**
 * update status method
 *
 * @throws NotFoundException
 * @throws MethodNotAllowedException
 * @param string $id
 * @return void
 */
	public function status($id = null,$status) {

	if($status == "closed")
	{
		$status = 'open';
		
		$ConsignmentItem = $this->loadModel('ConsignmentItem');
		
		$options = array (
					"conditions" => array (
							"ConsignmentItem.consignment_id" => $id,
							"Item.item_status_id" => 4,
					) 
			);
			
		$consignmentItem = $this->ConsignmentItem->find ( "all",$options );		
		for($i=0;$i<count($consignmentItem);$i++)
		{
			$Item = $this->loadModel('Item');
			$this->Item->id = $consignmentItem[$i]['Item']['id'];				
			$this->Item->saveField('item_status_id', 1);
		}	
		
	}
	else
	{
		$status = 'closed';
		
		$ConsignmentItem = $this->loadModel('ConsignmentItem');
		
		$options = array (
					"conditions" => array (
							"ConsignmentItem.consignment_id" => $id,
							"Item.item_status_id" => 1,
					) 
			);
			
		$consignmentItem = $this->ConsignmentItem->find ( "all",$options );		
		for($i=0;$i<count($consignmentItem);$i++)
		{
			$Item = $this->loadModel('Item');
			$this->Item->id = $consignmentItem[$i]['Item']['id'];				
			$this->Item->saveField('item_status_id', 4);
		}		
	}

	$this->Consignment->id = $id;			
	$this->Consignment->saveField('status', $status);	

	$this->Session->setFlash(__('Status Updated Successfully.'));
	$this->redirect(array('action' => 'index'));
	
	}
}



